/*
 * @Author: silence
 * @Date: 2022-07-21 16:17:21
 * @LastEditors: silence silence9685@163.com
 * @LastEditTime: 2023-08-01 14:52:45
 * @Description:  设置当前app：页面刷新组件
 */

import 'dart:io';

import 'package:ems_neutral_app/main.dart';
import 'package:ems_neutral_app/pages/Mine/Language/SilLocalizations/SilLocalizations.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';

import '../../../utils/eventBus/eventBus.dart';
import '../../../utils/string/StringConstant.dart';
import '../../utils/color/ColorUtils.dart';

mixin SmartRefresherInterfaceInstance implements SilSmartRefresherInterface {
  @override
  Widget refresherHeaderWidget() {
    // return ClassicHeader();
    // 使用主题色
    return ClassicHeader(
        failedIcon: Icon(Icons.error, color: ColorUtils.color_theme),
        completeIcon: Icon(Icons.done, color: ColorUtils.color_theme),
        idleIcon: Icon(Icons.arrow_downward, color: ColorUtils.color_theme),
        releaseIcon: Icon(Icons.refresh, color: ColorUtils.color_theme),
        textStyle: TextStyle(color: ColorUtils.color_theme),
        refreshingIcon: SizedBox(
          width: 25.0,
          height: 25.0,
          child: Platform.isIOS
              ? CupertinoActivityIndicator(color: ColorUtils.color_theme)
              : CircularProgressIndicator(
                  strokeWidth: 2.0, color: ColorUtils.color_theme),
        ));
  }

  @override
  Widget refresherFooterWidget() {
    Widget footer;

    String? noMorePprompt =
        LangOf(navigatorKey.currentState!.context)?.common?.noMore;

    footer = CustomFooter(builder: (context, loadStatus) {
      if (loadStatus == LoadStatus.failed) {
        footer = SilText('Loading failed, please try again',
            textAlign: TextAlign.center);
        // footer = SilText('加载失败，请重试', textAlign: TextAlign.center);
      } else if (loadStatus == LoadStatus.loading) {
        footer = CupertinoActivityIndicator();
      } else if (loadStatus == LoadStatus.noMore) {
        footer = SilText(
          noMorePprompt, //'没有更多了',
          textAlign: TextAlign.center,
          style: TextStyle(fontSize: 12, color: ColorUtils.color_theme),
        );
      } else {
        return SilText('Pull up to load more', // Pull up to load more 上拉加载更多
            textAlign: TextAlign.center,
            style: TextStyle(
                fontSize: 12,
                color: ColorUtils.color_theme)); //ClassicFooter();
      }

      return Container(
        height: 55.0,
        child: Center(child: footer),
      );
    });
    return footer;
  }

  @override
  bool noMoreData({required result, required int loadedDataNum}) {
    if (result.count != null) {
      return result.count <= loadedDataNum;
    }
    return false;
  }

  @override
  Widget loadingWidget() {
    String? promptTitle =
        LangOf(navigatorKey.currentState!.context)?.common?.loading;
    return SilPageLoadingWidget(
        indicatorColor: ColorUtils.color_theme, promptTitle: promptTitle);
  }

  @override
  Widget netErrorWidget(
      {RefreshController? errorRefreshController,
      void Function()? reloadFunc}) {
    // 有网时，进行自动刷新
    bus.on(StringConstant.NetStateChange, (network) {
      if (network) {
        if (errorRefreshController != null) {
          errorRefreshController.requestRefresh();
        }
      }
    });

    String? regainPrompt =
        LangOf(navigatorKey.currentState!.context)?.common?.regain;

    String? prompt =
        LangOf(navigatorKey.currentState!.context)?.netErr?.net_err;

    return SilNetErrorWidget(
        backgroundColor: ColorUtils.color_bg,
        promptTextStyle:
            TextStyle(fontSize: 14, color: ColorUtils.color_font_main),
        textStyle: TextStyle(fontSize: 14, color: ColorUtils.color_theme),
        promptMargin: EdgeInsets.only(top: 10),
        buttonMargin: EdgeInsets.only(top: 10),
        refreshController: errorRefreshController,
        reloadFunc: reloadFunc,
        bottomButtonTitle: regainPrompt, //"点击屏幕重试",
        prompt: prompt //'网络不给力'
        );
  }

  @override
  Widget noDataWidget(
      {RefreshController? emptyRefreshController,
      void Function()? reloadFunc}) {
    String? prompt =
        LangOf(navigatorKey.currentState!.context)?.faultList?.no_data;

    return Container(
        alignment: Alignment.center,
        child:
            SilText(prompt, fontSize: 14.w, color: ColorUtils.color_subtitle));
    // return SilEmptyDataWidget(
    //   emptyData:SilEmptyData(prompt: LangOf(navigatorKey.currentState!.context)?.faultList?.no_data,//;'暂无数据'
    //   imagePath:'',//'imgs/common/common_nodata.png'
    //   ),
    //     backgroundColor: ColorUtils.color_bg,
    //     promptTextStyle:
    //         TextStyle(fontSize: 14, color: ColorUtils.color_font_main),
    //     refreshController: emptyRefreshController,
    //     reloadFunc: reloadFunc);
  }
}
